﻿#include "CustomTabview.h"
#include "styleDefine.h"
#include <QVBoxLayout>
#include <QDebug>

#define TAB_DEFAULT_HEIGHT  30
#define TAB_MAX_SIZE        10

/***********************************************************/
CustomTabView::CustomTabView(QWidget* parent)
    : QFrame(parent)
    , m_bShowTabIndex(false)
    , m_pTabIndex(nullptr)
    , m_pTabIdx(nullptr)
    , m_pTabViews(nullptr)
{
    m_vecTabIndex.clear();

    QVBoxLayout* pBox = new QVBoxLayout;
    pBox->setContentsMargins(0, 0, 0, 0);
    pBox->setSpacing(0);
    this->setLayout(pBox);
    m_pTabIndex = new QFrame(this);
    m_pTabViews = new QStackedWidget(this);

    // 标签栏设置
    m_pTabIndex->setFixedHeight(TAB_DEFAULT_HEIGHT);
    m_pTabIndex->setStyleSheet(PAGE_COMMON_SELECTED_COLOUR);
    QHBoxLayout* pTabLayout = new QHBoxLayout;
    m_pTabIndex->setLayout(pTabLayout);
    pTabLayout->setContentsMargins(0, 0, 0, 0);
    pTabLayout->addStretch();

    QPushButton* pDelOne = new QPushButton(this);
    pDelOne->setFixedSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pDelOne->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pDelOne->setIcon(QIcon(":/common/resource/common/delete.ico"));
    pDelOne->setIconSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pDelOne->setStatusTip(QString("删除当前标签页"));
    pTabLayout->addWidget(pDelOne);

    m_pTabIdx = new QHBoxLayout;
    pTabLayout->addLayout(m_pTabIdx);

    QPushButton* pNewOne = new QPushButton(this);
    pNewOne->setFixedSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pNewOne->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pNewOne->setIcon(QIcon(":/common/resource/common/add.ico"));
    pNewOne->setIconSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pNewOne->setStatusTip(QString("新增标签页"));
    pTabLayout->addWidget(pNewOne);
    
    pTabLayout->addStretch();

    connect(pNewOne, SIGNAL(clicked()), this, SLOT(slt_newTabIdx()));
    connect(pDelOne, SIGNAL(clicked()), this, SLOT(slt_delTabIdx()));

    pBox->addWidget(m_pTabIndex);
    pBox->addWidget(m_pTabViews);

    m_pTabIndex->hide();
}

void CustomTabView::showTabIndex()
{
    m_bShowTabIndex = !m_bShowTabIndex;
    if (true == m_bShowTabIndex)
    {
        m_pTabIndex->show();
    }
    else
    {
        m_pTabIndex->hide();
    }
}

void CustomTabView::newTab()
{
    switch (m_nType)
    {
    case FUNC_FORM_BASE64:
    {
        FormBase64Proc* pFormBase64Proc = new FormBase64Proc(this);
        m_pTabViews->addWidget(pFormBase64Proc);
        break;
    }
    case FUNC_FORM_PICPROC:
    {
        FormPicProc* pFormPicProc = new FormPicProc(this);
        m_pTabViews->addWidget(pFormPicProc);
        break;
    }
    default:
        break;
    }

    m_pTabViews->setCurrentIndex(m_pTabViews->count() - 1); // 设置刚创建的页面显示出来

    //qDebug() << "TabViewsSize " << m_pTabViews->count() << " cur: " << m_pTabViews->currentIndex();
}

void CustomTabView::addTabType(const FUNC_FORM_TYPE nType)
{
    if (nullptr == m_pTabViews)
    {
        return;
    }

    m_nType = nType;

    slt_newTabIdx();
}

void CustomTabView::slt_newTabIdx()
{
    const uint nTabSize = m_vecTabIndex.size();
    if (TAB_MAX_SIZE <= nTabSize)
    {
        return;
    }

    for (auto& it : m_vecTabIndex)
    {
        it->setNoSelect();
        it->setIcon(QIcon(QString(":/number/resource/number/bule_%1.ico").arg(it->getIndex())));
    }

    FluButton* pOneTab = new FluButton(this);
    pOneTab->setIndex(nTabSize);
    pOneTab->setFixedSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pOneTab->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pOneTab->setIcon(QIcon(QString(":/number/resource/number/green_%1.ico").arg(nTabSize)));
    pOneTab->setIconSize(QSize(TAB_DEFAULT_HEIGHT, TAB_DEFAULT_HEIGHT));
    pOneTab->setSelected();
    connect(pOneTab, SIGNAL(click_index(uint)), this, SLOT(slt_selectTabIdx(uint)));

    m_pTabIdx->addWidget(pOneTab);
    m_vecTabIndex.push_back(pOneTab);

    newTab();
}

void CustomTabView::slt_delTabIdx()
{
    // 标签页均为空，退出
    if (m_vecTabIndex.isEmpty())
    {
        return;
    }

    // 先获取具体被标记选中的页面序号
    uint nSelectIndex = INVALID_VALUE;
    for (const auto& it : m_vecTabIndex)
    {
        if (true == it->getSelected())
        {
            nSelectIndex = it->getIndex();
            //qDebug() << "slt_delTabIdx find >>>> " << nSelectIndex;
            break;
        }
    }

    //qDebug() << "slt_delTabIdx " << nSelectIndex;

    if (INVALID_VALUE == nSelectIndex)
    {
        // 无效，存在异常，报错
        return;
    }

    // 将标签页先删除最后一个
    QPushButton* pOneTab = m_vecTabIndex.back();
    if (nullptr != pOneTab)
    {
        m_pTabIdx->removeWidget(pOneTab);
        m_vecTabIndex.remove(m_vecTabIndex.size() - 1); // 移除最后一个
        delete pOneTab;
        pOneTab = nullptr;
    }

    // 删除标签页内容并移动需展示的
    QWidget* pObj = m_pTabViews->currentWidget();
    m_pTabViews->removeWidget(pObj);
    if (nSelectIndex > 0)
    {
        m_pTabViews->setCurrentIndex(nSelectIndex - 1);
        m_vecTabIndex[nSelectIndex - 1]->setSelected();
    }
    else
    {
        if(false == m_vecTabIndex.isEmpty())
        {
            m_pTabViews->setCurrentIndex(0);
            m_vecTabIndex[0]->setSelected();
        }
    }

    reset();
}

void CustomTabView::slt_selectTabIdx(uint nIndex)
{
    //qDebug() << "slt_selectTabIdx " << nIndex;
    for (auto& it : m_vecTabIndex)
    {
        if(nIndex != it->getIndex())
        {
            it->setNoSelect();
        }
    }

    m_pTabViews->setCurrentIndex(nIndex);
    reset();
}

void CustomTabView::reset()
{
    for (auto& it : m_vecTabIndex)
    {
        if (true == it->getSelected())
        {
            it->setIcon(QIcon(QString(":/number/resource/number/green_%1.ico").arg(it->getIndex())));
        }
        else
        {
            it->setIcon(QIcon(QString(":/number/resource/number/bule_%1.ico").arg(it->getIndex())));
        }
    }
}
